package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @description: 会员业务层
 * @author: wangjingliang
 * @create: 2019-11-20 19:55
 **/
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录操作
     * @param:a
     * @return: null
     * @date: 2019/11/20
     */
    @Override
    public Member login(String telephone) {
        //用手机号去会员表查询是否存在
        Member member = null;
        try {
            member = memberMapper.findMemberByTelePhone(telephone);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }

        if (member==null){
            member.setName(ValidateCodeUtils.generateValidateCode4String(6));
            //不存在,则说明不是会员,进行会员注册
            member.setPhoneNumber(telephone);
            String password="123456";
            //对密码进行加密
            member.setPassword(MD5Utils.md5(password));
            try {
                memberMapper.addAndReturnId(member);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(MessageConstant.EXCEPION);
            }
        }
        //无聊结果如何都要返回前端,手机号对应的验证码
        return member;
    }
}
